package com.geekhalo.admin.auth;

import com.geekhalo.admin.app.user.UserQueryApplication;
import com.geekhalo.admin.domain.user.UserStatus;
import com.geekhalo.admin.domain.user.UserView;
import com.geekhalo.admin.domain.user.query.GetByLoginName;
import com.geekhalo.lego.core.web.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("auth")
public class AuthController {
    @Autowired
    private UserQueryApplication userQueryApplication;

    @PostMapping("login")
    public RestResult<String> login(@RequestBody GetByLoginName command){
        UserView userView = userQueryApplication.get(command);
        if (userView == null){
            return RestResult.fail("登录失败");
        }
        if (userView.getStatus() != UserStatus.ENABLED){
            return RestResult.fail("用户已禁用");
        }
        if (!userView.checkPassword(command.getPassword())){
            return RestResult.fail("登录失败");
        }
        return RestResult.success();
    }
}
